package com.example.blank700.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends AppCompatActivity {
    private String[] weekSrt = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        //创建数组适配器并在ArrayAdapter的构造函数中传入适配的数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                ListViewActivity.this, android.R.layout.simple_list_item_1, weekSrt
        );
        ListView listView = (ListView) findViewById(R.id.listview);
        //设置适配器
        listView.setAdapter(adapter);

        //设置监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListViewActivity.this.setTitle(((TextView) view).getText());
                Toast.makeText(ListViewActivity.this, "Set title as " + ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
